package cu.ds.logicalsync.fytasrealm.ui;

/*
 * DesktopApplication1View.java
 */

import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;

import cu.ds.logicalsync.fytasrealm.FytasRealmDesktop;
import cu.ds.logicalsync.fytasrealm.MoveDirection;
import cu.ds.logicalsync.fytasrealm.View;
import cu.ds.logicalsync.fytasrealm.communication.MapStatusService;
import cu.ds.logicalsync.fytasrealm.communication.Network;
import cu.ds.logicalsync.fytasrealm.communication.event.MapStatusEvent;
import cu.ds.logicalsync.fytasrealm.communication.event.MapStatusListener;
import cu.ds.logicalsync.fytasrealm.model.Map;
import cu.ds.logicalsync.fytasrealm.model.PositionMap;
import cu.ds.logicalsync.fytasrealm.model.PositionWorld;
import cu.ds.logicalsync.fytasrealm.model.Warrior;
import cu.ds.logicalsync.fytasrealm.model.World;
import cu.ds.logicalsync.fytasrealm.util.Resources;
import cu.ds.logicalsync.fytasrealm.model.PositionColor;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.ImageIcon; // oat
import java.awt.image.*; // oat
import java.awt.*; 
import javax.swing.JDialog;
import javax.swing.JFrame;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

/**
 * The application's main frame.
 */
public class MainWindow extends FrameView implements View {

    public MainWindow(SingleFrameApplication app) {
        super(app);

        initComponents();

        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });

        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String)(evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer)(evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });
        ///--- oat
        //drawItAll(); //<-- Tay: do not call in the constructor!
    }

    public void drawItAll(){
        startWorld();
        startMap();
        drawWarrior();
    }
    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = FytasRealmDesktop.getApplication().getMainFrame();
            // TODO: Migrate AboutBox from NetBean
            //aboutBox = new DesktopApplication1AboutBox(mainFrame);
            //aboutBox.setLocationRelativeTo(mainFrame);
        }
        FytasRealmDesktop.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        mapTab = new javax.swing.JPanel();
        GlobalConsole = new javax.swing.JTextArea();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        mapButton = new javax.swing.JButton();
        worldTab = new javax.swing.JPanel();
        worldButton = new javax.swing.JButton();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();
        jFrame1 = new javax.swing.JFrame();

        mainPanel.setName("mainPanel"); // NOI18N

        jTabbedPane1.setName("jTabbedPane1"); // NOI18N

        mapTab.setName("mapTab"); // NOI18N

        GlobalConsole.setColumns(20);
        GlobalConsole.setRows(5);
        GlobalConsole.setEnabled(false);
        GlobalConsole.setName("GlobalConsole"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(FytasRealmDesktop.class).getContext().getResourceMap(MainWindow.class);
        jTextField1.setText(resourceMap.getString("jTextField1.text")); // NOI18N
        jTextField1.setName("jTextField1"); // NOI18N

        jTextField2.setText(resourceMap.getString("jTextField2.text")); // NOI18N
        jTextField2.setName("jTextField2"); // NOI18N

        jTextField3.setText(resourceMap.getString("jTextField3.text")); // NOI18N
        jTextField3.setName("jTextField3"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        mapButton.setText(resourceMap.getString("mapButton.text")); // NOI18N
        mapButton.setContentAreaFilled(false);
        mapButton.setName("mapButton"); // NOI18N
        mapButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                mapButtonMousePressed(evt);
            }
        });
        jScrollPane1.setViewportView(mapButton);

        org.jdesktop.layout.GroupLayout mapTabLayout = new org.jdesktop.layout.GroupLayout(mapTab);
        mapTab.setLayout(mapTabLayout);
        mapTabLayout.setHorizontalGroup(
            mapTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(mapTabLayout.createSequentialGroup()
                .addContainerGap()
                .add(mapTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(GlobalConsole, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 597, Short.MAX_VALUE)
                    .add(mapTabLayout.createSequentialGroup()
                        .add(mapTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel1)
                            .add(jLabel2)
                            .add(jLabel3)
                            .add(jLabel4))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(mapTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(jTextField1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)
                            .add(jTextField2)
                            .add(jTextField3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(18, 18, 18)
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 389, Short.MAX_VALUE)))
                .addContainerGap())
        );
        mapTabLayout.setVerticalGroup(
            mapTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(mapTabLayout.createSequentialGroup()
                .addContainerGap()
                .add(GlobalConsole, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 99, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(mapTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(mapTabLayout.createSequentialGroup()
                        .add(jLabel4)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(mapTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel1)
                            .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(mapTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel2)
                            .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(mapTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel3)
                            .add(jTextField3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 920, Short.MAX_VALUE))
                .addContainerGap())
        );

        jTabbedPane1.addTab(resourceMap.getString("mapTab.TabConstraints.tabTitle"), mapTab); // NOI18N

        worldTab.setName("worldTab"); // NOI18N
        worldTab.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                worldTabKeyPressed(evt);
            }
        });

        worldButton.setIcon(resourceMap.getIcon("worldButton.icon")); // NOI18N
        worldButton.setText(resourceMap.getString("worldButton.text")); // NOI18N
        worldButton.setContentAreaFilled(false);
        worldButton.setName("worldButton"); // NOI18N
        worldButton.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                worldButtonKeyPressed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout worldTabLayout = new org.jdesktop.layout.GroupLayout(worldTab);
        worldTab.setLayout(worldTabLayout);
        worldTabLayout.setHorizontalGroup(
            worldTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(worldButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 617, Short.MAX_VALUE)
        );
        worldTabLayout.setVerticalGroup(
            worldTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(worldButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 1047, Short.MAX_VALUE)
        );

        worldButton.getAccessibleContext().setAccessibleName(resourceMap.getString("jButton1.AccessibleContext.accessibleName")); // NOI18N

        jTabbedPane1.addTab(resourceMap.getString("worldTab.TabConstraints.tabTitle"), worldTab); // NOI18N

        org.jdesktop.layout.GroupLayout mainPanelLayout = new org.jdesktop.layout.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jTabbedPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 622, Short.MAX_VALUE)
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jTabbedPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 1075, Short.MAX_VALUE)
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(FytasRealmDesktop.class).getContext().getActionMap(MainWindow.class, this);
        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        org.jdesktop.layout.GroupLayout statusPanelLayout = new org.jdesktop.layout.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(statusPanelSeparator, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 622, Short.MAX_VALUE)
            .add(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(statusMessageLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 452, Short.MAX_VALUE)
                .add(progressBar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(statusPanelLayout.createSequentialGroup()
                .add(statusPanelSeparator, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(statusPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(statusMessageLabel)
                    .add(statusAnimationLabel)
                    .add(progressBar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(11, 11, 11))
        );

        jFrame1.setName("jFrame1"); // NOI18N

        org.jdesktop.layout.GroupLayout jFrame1Layout = new org.jdesktop.layout.GroupLayout(jFrame1.getContentPane());
        jFrame1.getContentPane().setLayout(jFrame1Layout);
        jFrame1Layout.setHorizontalGroup(
            jFrame1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 400, Short.MAX_VALUE)
        );
        jFrame1Layout.setVerticalGroup(
            jFrame1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 300, Short.MAX_VALUE)
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>                        
    private void startMap(){
    	//Tay: Register MapStatusServiceListener
    	Network net = getFytasRealm().getNetWork();
    	MapStatusService mapStatusService = net.getMapStatusService();
    	mapStatusService.addListener(new MapStatusListener() {
			public void mapUpdated(MapStatusEvent event) {
				// TODO drawMap here!
			}
    	});
    	
    	drawMap();
    }

    private void drawMap(){

        mapImage.createGraphics();
        Graphics2D g = (Graphics2D)mapImage.getGraphics();
        g.setColor(Color.WHITE);
        //g.fillRect(0, 0, 800, 600);
        g.setColor(Color.BLACK);
        PositionMap allPosition[][] = myMap.getMapRoom();
        for(int i = 0;i < 400 ;i++){
            for(int j = 0;j < 300 ;j++){

            	switch(allPosition[i][j].getPositionColor()){
            		case Green:{
            			g.setColor(Color.GREEN);
            		}
            		case Red:{
            			g.setColor(Color.RED);
            		}
            		case Blue:{
            			g.setColor(Color.BLUE);
            		}default:{
            			
                    	if((i+j)%2 == 0){
                            g.setColor(new Color(200,200, 200));
                        }
                        else{
                            g.setColor(Color.WHITE);
                        }
            		}
            	}
                g.fillRect(i*mapsize, j*mapsize,mapsize,mapsize);
            }
        }
        ////// draw my current position in map
        World myCurrentWorld = myWarrior.getMyCurrentWorld();
        PositionMap myCurrentWorldPosition = myCurrentWorld.getMyPositionInMap();
        int myCurrentPositionX = myCurrentWorldPosition.colPosition ; 
        int myCurrentPositionY = myCurrentWorldPosition.rowPosition ; 

		g.setColor(Color.BLUE);
        g.fillRect(myCurrentPositionX*mapsize, myCurrentPositionY*mapsize
        		,mapsize,mapsize);
        ////// draw my world position in map
        World myWorld = myWarrior.getMyWorld();
        PositionMap myWorldPosition = myWorld.getMyPositionInMap();
        int myPositionX = myWorldPosition.colPosition ; 
        int myPositionY = myWorldPosition.rowPosition ; 
        g.setColor(Color.BLACK);
        g.fillRect(myPositionX*mapsize, myPositionY*mapsize
        		,1,mapsize);
        g.fillRect(myPositionX*mapsize, myPositionY*mapsize
        		,mapsize,1);
        g.fillRect(myPositionX*mapsize, myPositionY*mapsize+mapsize
        		,mapsize,1);
        g.fillRect(myPositionX*mapsize+mapsize, myPositionY*mapsize
        		,1,mapsize);
        //////////////
        
        mapImageIcon.setImage(mapImage);
        mapButton.setIcon(mapImageIcon);
        //World myCurrent = myWarrior.getMyCurrentWorld();
        ////// ----- lazy to test
        worldButton.repaint();
        worldTab.repaint();
        jFrame1.repaint();

    }

  
    private void startWorld(){
        drawWorld();
    }
    private void drawWorld(){
        
        worldImage.createGraphics();
        Graphics2D g = (Graphics2D)worldImage.getGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, 401, 401);
        g.setColor(Color.BLACK);
        
        ArrayList<PositionWorld> warriorPlaces = myWorld.findOccupiedPositions();
        

        PositionWorld allPosition[][] = myWorld.getWorldRoom();
        for(int i = 0;i < 8 ;i++){
            for(int j = 0;j < 8 ;j++){
                if((i+j)%2 == 0){
                    g.setColor(new Color(200,200, 200));
                }
                else{
                    g.setColor(Color.WHITE);
                }
                g.fillRect(i*worldsize, j*worldsize,
                        i*worldsize+worldsize,j*worldsize+worldsize);
//                if((i+j)%2 == 0){
//                    g.setColor(Color.WHITE);
//                }
//                else{
//                    g.setColor(new Color(200,200, 200));
//                }
                g.setColor(Color.BLACK);
                String myWarName = allPosition[i][j].getWarriorNameInWorld();
                System.out.println( "::"+i+"-"+j+myWarName);
                myWarName = (myWarName == null) ? "" : myWarName ;
                Font font = new Font("Arial", Font.PLAIN, 20);
                g.setFont(font);
                g.drawString(myWarName, i*worldsize, 
                		j*worldsize+10+20);
                
                //g.drawString(""+i+""+j, i*worldsize+15, 
                //		j*worldsize+10+20);
                
            }
        }
        

        worldImageIcon.setImage(worldImage);
        worldButton.setIcon(worldImageIcon);

        ////// ----- lazy to test
        worldButton.repaint();
        worldTab.repaint();
        jFrame1.repaint();

    }

    private void worldTabKeyPressed(java.awt.event.KeyEvent evt) {                                    
        // TODO add your handling code here:
        //KeyEvent.VK_KP_DOWN;
        switch(evt.getKeyCode()){
        	case 40:
            case KeyEvent.VK_KP_DOWN : 
            	getFytasRealm().move(MoveDirection.Down,this.myWarrior);
            	break;
            case 37:
            case KeyEvent.VK_KP_LEFT : 
            	getFytasRealm().move(MoveDirection.Left,this.myWarrior);
            	break;
            case 39:
            case KeyEvent.VK_KP_RIGHT  : 
            	getFytasRealm().move(MoveDirection.Right,this.myWarrior);
            	break;
            case 38:
            case KeyEvent.VK_KP_UP  : 
            	getFytasRealm().move(MoveDirection.Up,this.myWarrior);
            	break;
        }
        drawWorld();
        //System.out.print(evt.getKeyText(evt.getKeyCode()));
        //drawWorld();   
}                                   

    private void worldButtonKeyPressed(java.awt.event.KeyEvent evt) {                                       
        // TODO add your handling code here:
        worldTabKeyPressed(evt);
}                                      

    private void mapButtonMousePressed(java.awt.event.MouseEvent evt) {                                       
        // TODO add your handling code here:
        int myX = evt.getX()-16;
        myX /= 5 ;
        int myY = evt.getY()-5;
        myY /= 5 ;

        System.out.print("===" + myX +" === "+ myY );
        getFytasRealm().changeWorld(myX, myY);
    }                                      

    // Variables declaration - do not modify                     
    private javax.swing.JTextArea GlobalConsole;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JButton mapButton;
    private javax.swing.JPanel mapTab;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    private javax.swing.JButton worldButton;
    private javax.swing.JPanel worldTab;
    // End of variables declaration                   

    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;
    private int worldsize = 50;
    private int mapsize = 5;

    private JDialog aboutBox;
    private ImageIcon worldImageIcon = new ImageIcon();
    private BufferedImage worldImage = new BufferedImage(400, 400, BufferedImage.TYPE_INT_RGB);
    private ImageIcon mapImageIcon = new ImageIcon();
    private BufferedImage mapImage = new BufferedImage(400*mapsize, 300*mapsize, BufferedImage.TYPE_INT_RGB);
    
    
    /************* Non generated code *************/
    
    ///// oat new method
    private Warrior myWarrior;
    private Map myMap;
    private World myWorld;
    private static PositionColor pColor;
    
    private FytasRealmDesktop getFytasRealm() {
    	return (FytasRealmDesktop)this.getApplication();
    }
    public void setWarrior(Warrior w){
        myWarrior = w;
    }
    public void setMap(Map m){
        myMap = m;
    }
    public void setWorld(World w){
        myWorld = w;
    };

    public boolean updateWarrior(){
        drawWarrior();
        return true;
    }
    public boolean updateMap(){
        drawMap();
        return true;
    }
    public boolean updateWorld(){
        drawWorld();
        return true;
    }
    public boolean updateLog(String myLog){
        GlobalConsole.setText(myLog);
        return true;
    }
    private void drawWarrior(){
        //myWarrior.XXXXX
    	
        String level = "Level "+myWarrior.getWarriorLevel();
        String name =  myWarrior.getWarriorName();
        String hp = ""+myWarrior.getWarriorHp();
        String currentWorld = myWarrior.getMyCurrentWorld().getWorldName();
        
        jTextField1.setText(name);
        jTextField2.setText(hp);
        jTextField3.setText(currentWorld);
        jLabel4.setText(level);

//        jTextField2
//        jTextField3
//        jLabel4


    }
    ////////////////////////////////////////////////
    
    
    /************ View's action methods ***************/
    
	public String openLoginDialog() {
		return (String)JOptionPane.showInputDialog(null, "Please enter your name", "Login",
				  JOptionPane.QUESTION_MESSAGE, null, null, "Name");
	}

	public void setClientName(String clientName) {
		//TODO: Set Title to FrameView
		//this.setTitle(Resources.getString("title.prototype") + " : " + clientName);
		//this.
	}
	
	public void changeWorld(int row, int col) {
		
	}
    
}
